package com.zrt.pad.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.telephony.TelephonyManager;

import com.zrt.pad.commonui.SharedApplication;

public class TelUtil {
	public void setMobileNetEnable(boolean enable) {
		try {
			invokeBooleanArgMethod("setMobileDataEnabled", enable);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean getMobileNetEnable() {
		Context c = SharedApplication.context;
		ConnectivityManager mConnectivityManager = (ConnectivityManager) c
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		Class<? extends ConnectivityManager> ownerClass = mConnectivityManager
				.getClass();
		@SuppressWarnings("rawtypes")
		Class[] argsClass = {};
		Boolean isOpen = false;
		try {
			Method method = ownerClass.getMethod("getMobileDataEnabled",
					argsClass);
			isOpen = (Boolean) method.invoke(mConnectivityManager);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return isOpen;
	}

	public Object invokeBooleanArgMethod(String methodName, boolean value)
			throws Exception {
		Context c = SharedApplication.context;
		ConnectivityManager mConnectivityManager = (ConnectivityManager) c
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		Class<? extends ConnectivityManager> ownerClass = mConnectivityManager
				.getClass();

		@SuppressWarnings("rawtypes")
		Class[] argsClass = new Class[1];
		argsClass[0] = boolean.class;

		Method method = ownerClass.getMethod(methodName, argsClass);

		return method.invoke(mConnectivityManager, value);
	}

	private static Uri PREFERRED_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");
	public static final Uri APN_LIST_URI = Uri
			.parse("content://telephony/carriers");
	public static final String APN_ID = "apn_id";

	// private static final int ID_INDEX = 0;

	/**
	 * get apntype
	 * 
	 * @param context
	 * @return
	 */
	public static String getApnType(Context context) {
		String apntype = "NoMatch";
		Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null,
				null, null, null);
		if (c != null && c.getCount() >= 1) {
			c.moveToFirst();
			String user = c.getString(c.getColumnIndex("user"));
			// if(user.startsWith(APNNET.CTNET)){
			// apntype=APNNET.CTNET;
			// }else if(user.startsWith(APNNET.CTWAP)){
			// apntype=APNNET.CTWAP;
			// }
			apntype = user;
		}
		c.close();
		return apntype;
	}

	public static void setSelectedApnKey(String id) {
		ContentResolver resolver = SharedApplication.context
				.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(APN_ID, id);
		resolver.update(PREFERRED_APN_URI, values, null, null);
	}

	public String getCurrentAPNFromSetting() {
		ContentResolver resolver = SharedApplication.context
				.getContentResolver();
		Cursor cursor = null;
		try {
			cursor = resolver.query(PREFERRED_APN_URI, null, null, null, null);
			String curApnId = null;
			if (cursor != null && cursor.moveToFirst()) {
				curApnId = cursor.getString(cursor.getColumnIndex("apn_id"));
				// String apnName1 =
				// cursor.getString(cursor.getColumnIndex("apn"));
			}
			cursor.close();
			// find apn name from apn list
			if (curApnId != null) {
				cursor = resolver.query(APN_LIST_URI, null, " _id = ?",
						new String[] { curApnId }, null);
				if (cursor != null && cursor.moveToFirst()) {
					String apnName = cursor.getString(cursor
							.getColumnIndex("apn"));
					return apnName;
				}
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}

		return null;
	}

	@SuppressLint("DefaultLocale")
	public int updateCurrentAPN(String newAPN) {
		ContentResolver resolver = SharedApplication.context
				.getContentResolver();
		Cursor cursor = null;
		try {
			// get new apn id from list
			cursor = resolver.query(APN_LIST_URI, null,
					" apn = ? and current = 1",
					new String[] { newAPN.toLowerCase() }, null);
			String apnId = null;
			if (cursor != null && cursor.moveToFirst()) {
				apnId = cursor.getString(cursor.getColumnIndex("_id"));
			}
			cursor.close();

			// set new apn id as chosen one
			if (apnId != null) {
				ContentValues values = new ContentValues();
				values.put("apn_id", apnId);
				resolver.update(PREFERRED_APN_URI, values, null, null);
			} else {
				// apn id not found, return 0.
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		// update success
		return 1;
	}

	public static final int APN_NET = 0;
	public static final int APN_WAP = 1;
	public static final int APN_MMSC = 2;
	
	public String getSimOperator(){
		Context c = SharedApplication.context;
		TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) {
			return tm.getSimOperator();
		} else {
			return null;
		}
	}
	
	public boolean createApn(int apn){
		String op = getSimOperator();
		if (op == null) {
			return false;
		}
		ContentValues cv = new ContentValues();
		switch (apn) {
		case APN_NET:
		{
			cv.put("name","NET"); //apn中文描述
			cv.put("apn", "ctnet");  //apn名称
			cv.put("type", "default");
			cv.put("numeric", op);
			cv.put("mcc", op.substring(0, 3));
			cv.put("mnc", op.substring(3, op.length()));
			cv.put("proxy", "10.0.0.200");
			cv.put("port", "80");
			cv.put("mmsproxy", "");
			cv.put("mmsport", "");
			cv.put("user", "");
			cv.put("server", "");
			cv.put("password", "");
			cv.put("mmsc", "");
		}
			break;
		case APN_WAP:
		{
			cv.put("name","WAP"); //apn中文描述
			cv.put("apn", "ctwap");  //apn名称
			cv.put("type", "default");
			cv.put("numeric", op);
			cv.put("mcc", op.substring(0, 3));
			cv.put("mnc", op.substring(3, op.length()));
			cv.put("proxy", "10.0.0.200");
			cv.put("port", "80");
			cv.put("mmsproxy", "");
			cv.put("mmsport", "");
			cv.put("user", "");
			cv.put("server", "");
			cv.put("password", "");
			cv.put("mmsc", "");
		}
			break;
		case APN_MMSC:
		{
			cv.put("name","彩信"); //apn中文描述
			cv.put("apn", "ctwap");  //apn名称
			cv.put("type", "mms");
			cv.put("numeric", op);
			cv.put("mcc", op.substring(0, 3));
			cv.put("mnc", op.substring(3, op.length()));
			cv.put("proxy", "10.0.0.200");
			cv.put("port", "80");
			cv.put("mmsproxy", "10.0.0.200");
			cv.put("mmsport", "");
			cv.put("user", "");
			cv.put("server", "");
			cv.put("password", "");
			cv.put("mmsc", "");
		}
			break;
		default:
			break;
		}
		Context c = SharedApplication.context;
		try {
			c.getContentResolver().insert(APN_LIST_URI, cv);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 电信手机应该是MEID
	 * @return
	 */
	public String getIMSI(){
		TelephonyManager tm = (TelephonyManager) SharedApplication.context.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSubscriberId();
	}
}
